<template>
  <div class="orderList">
    <div class="head">
      <div style="flex:4;padding-left:20px;">
        <span style="flex:2;">宝贝</span>
        <span style="flex:1;">单价</span>
        <span style="flex:1;">数量</span>
      </div>
      <div style="flex:4;padding-right:20px;">
        <span style="flex:1;">退款金额</span>
        <span style="flex:1;">申请时间</span>
        <span style="flex:1;">状态</span>
        <span style="flex:1;">操作</span>
      </div>
    </div>
    <div v-if="afterSaleList?.length">
      <div class="content" v-for="(item, index) in afterSaleList" :key="index">
        <div class="top">
          <div class="left">
            <span class="shopName" @click="toStore(item.shopId)">
              <img :src="item.shopLogo" alt="" />{{ item.shopName }}</span>
            <span class="orderCode">售后编号：{{ item.afterFormid }}</span>
          </div>
        </div>
        <div class="productBox">
          <div class="product">
            <div class="left fs13">
              <div class="box" v-for="(pro, inx) in item.skus" :key="inx">
                <div class="desc pointer" @click="toProductDetail(pro, item.shopId)">
                  <img :src="pro.image" alt="" />
                  <div>
                    <p class="name">{{ pro.productName }}</p>
                    <p class="size font-color-999"><span v-for="(val, idx) in pro.values" :key="idx">{{ val }}</span></p>
                  </div>
                </div>
                <div class="price">¥{{ pro.price }}</div>
                <div class="num">{{ pro.number }}</div>
              </div>
            </div>
            <div class="right" :class="item.skus.length > 1 ? 'right_line' : ''">
              <div class="actualPay">
                <div>
                  <p class="fs13 mar-bot-5">¥{{ item.price }}</p>
                </div>
              </div>
              <div class="applicationTime">
                {{ item.createTime }}
              </div>
              <div class="status">
                <p class="fs13 mar-bot-5">
                  {{ formateAfterState(item.afterState) }}
                </p>
              </div>
              <div class="operate">
                <el-button size="small" @click="toAfterSaleDetail(item.afterId, item.orderId)">售后详情</el-button>
                <p
                  v-if="item.afterState == 6 || item.afterState == 8" class="fs13 tex-und pointer mar-top-10"
                  @click="platform(item.afterId, item.orderId)"
                >申请平台介入</p>
              </div>
            </div>
          </div>
        </div>
      </div>
      <el-pagination
        :hide-on-single-page="true"
        :current-page="page.page"
        :page-size="page.pageSize"
        :total="page.total"
        @current-change="val => $emit('handleCurrentChange', val)"
        background
        layout="prev, pager, next"
        style="justify-content: flex-end;"
      />
    </div>
    <div class="noorder" v-else>
      <svg-icon style="width: 240px; height: 240px; margin-bottom: 20px;" name="user-order-nodata" />
      <p class="fs20 font-color-999">你还没有售后订单哦～</p>
    </div>
    <el-dialog
      title="申请平台介入"
      v-model="visible.intervention"
      width="37%"
      center
    >
      <el-input type="textarea" :rows="7" placeholder="请输入内容" v-model="query.reason" maxlength="200" />
      <div class="itemBox upload">
        <el-upload
          :action="action"
          list-type="picture-card"
          :limit="3"
          :on-success="handleSuccess"
          :on-remove="handleRemove">
          <el-icon><Plus /></el-icon>
          <span>最多上传3张</span>
        </el-upload>
      </div>
      <template #footer>
        <el-button type="primary" @click="interventionFn">确 定</el-button>
        <el-button @click="closeIntervention">取 消</el-button>
      </template>
    </el-dialog>
  </div>
</template>
<script setup>
import { defineComponent, reactive } from 'vue'
import {
  requestPlatform
} from '@/api/user/afterSale.js'
import { useRouter } from 'vue-router'
import {
  formateAfterState
} from './formate.js'
import SvgIcon from '@/components/svgIcon/index.vue';
import { Plus } from '@element-plus/icons-vue'
import { ref } from 'vue'
import {
  upload
} from '@/api/upload.js'

defineComponent({
  name: 'AfterSaleListComponent'
})
/**
 * 
 */
defineProps({
  afterSaleList: {
    type: Array,
    default: () => []
  },
  page: {
    type: Object,
    default: () => ({
      page: 1,
      pageSize: 5,
      total: 0
    })
  }
})
/**
 * @event handleCurrentChange 分页切换 参数参考element-plus
 * Read More: https://element-plus.org/zh-CN/component/pagination.html#events
 */
defineEmits(['handleCurrentChange'])

const action = upload
const router = useRouter();
const urls = ref([])

let visible = reactive({
  intervention: false,
})

// 跳转到店铺
function toStore (id) {
  router.push({
    path: '/store',
    query: {
      shopData: { shopId: id }
    }
  })
}
// 跳转到售后详情
function toAfterSaleDetail (aid, oid) {
  router.push({
    path: '/orderDetail',
    query: {
      afterId: aid, // 售后id
      orderId: oid, // 订单id
      type: 3
    }
  })
}
// 跳转到商品详情
function toProductDetail (item, id) {
  let data = {
    productId: item.productId,
    skuId: item.skuId,
    shopId: id
  }
  router.push({
    path: '/productDetail',
    query: {
      proData: JSON.stringify(data)
    }
  })
}
let query = reactive({
  afterId: '', // 售后订单id
  orderId: '', // 订单id
  reason: '',
  image: '',
})
// 平台介入
function platform (afterId, orderId) {
  query.afterId = afterId
  query.orderId = orderId
  visible.intervention = true
}
function closeIntervention () {
  query.afterId = ''
  query.orderId = ''
  query.reason = ''
  query.image = ''
  visible.intervention = false
}
// 申请平台介入
function interventionFn () {
  if (urls.value.length === 0) {
    ElMessage.error('请上传图片!')
    return
  }
  query.image = urls.value.join(',')
  requestPlatform(query).then(response => {
    const res = response.data
    if (res.code === '200') {
      ElMessage.success('申请平台介入成功')
      visible.intervention = false
      window.location.reload()
    } else {
      ElMessage.error(res.message)
    }
  }).catch(err => {
    ElMessage.error(err.message)
  })
}
const handleSuccess = (res) => {
  urls.value.push(res.data.url)
}
const handleRemove = (file, fileList) => {
  urls.value = []
  for (var i in fileList) {
    urls.value.push(fileList[i].response.data.url)
  }
}
</script>
<style lang="scss" scoped>
.orderList {
  width: 100%;

  .noorder {
    width: 100%;
    text-align: center;
    padding: 100px 0;

    p {
      margin-bottom: 20px;
    }

    .el-button {
      background-color: $mainGlod;
      color: #FFFFFF;
      font-weight: normal;
      border-radius: 0;
    }
  }

  .itemBox{
      width: 580px;
      display: flex;
      margin: 10px;
      .title{
        min-width: 80px;
        line-height: 40px;
        text-align: right;
      }
      :deep(.el-input__icon){
        color: $mainGlod;
      }
  }

  .upload{
    display: flex;
    .title{
      line-height: 40px;
    }
    .el-upload--picture-card {
      height: 140px;
      background-color: #FFF;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      i{
        font-size: 32px;
        margin-bottom: 1px;
      }
      &:hover{
        i{
        color: $mainGlod;
        }
      }
    }
  }

  .head {
    box-sizing: border-box;
    width: 100%;
    height: 44px;
    background-color: #F1F2F7;
    display: flex;
    align-items: center;
    text-align: center;
    margin-bottom: 20px;

    div {
      display: flex;
    }
  }

  .content {
    width: 100%;
    margin-bottom: 20px;
    border: 1px solid #E5E5E5;
    border-bottom: 0;
    box-sizing: border-box;

    .top {
      width: 100%;
      font-size: 13px;
      height: 40px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      background-color: #F3F3F3;

      .left {
        display: flex;

        .shopName {
          margin-left: 20px;
          margin-right: 60px;
          cursor: pointer;
          display: flex;
          align-items: center;

          img {
            width: 16px;
            height: 16px;
            vertical-align: middle;
            margin-right: 5px;
          }
        }

        .time {
          margin-right: 60px;
        }
      }
    }

    .productBox {
      width: 100%;
      box-sizing: border-box;

      .product {
        width: 100%;
        display: flex;

        .left {
          flex: 4;

          .box {
            padding: 20px 0 20px 20px;
            display: flex;
            border-bottom: 1px solid #E5E5E5;

            .desc {
              flex: 2;
              display: flex;

              img {
                width: 86px;
                height: 86px;
                margin-right: 10px;
              }

              div {
                display: flex;
                flex-direction: column;
                justify-content: space-between;

                .name {
                  font-size: 14px;
                }

                .size {
                  font-size: 13px;

                  span {
                    margin-right: 10px;
                  }

                  span:last-child {
                    margin-right: 0;
                  }
                }
              }
            }

            .price,
            .num {
              flex: 1;
              display: flex;
              align-items: center;
              justify-content: center;
            }
          }
        }

        .right {
          padding: 20px 20px 20px 0;
          flex: 4;
          display: flex;
          border-bottom: 1px solid #E5E5E5;

          .actualPay,
          .applicationTime,
          .status,
          .operate {
            flex: 1;
            display: flex;
            justify-content: center;
            align-items: center;
            text-align: center;

            div {
              text-align: center;
            }

            .el-button {
              border: 1px solid $mainGlod;
              color: $mainGlod;
              border-radius: 0;
            }
          }

          .status {
            p {
              color: #C83732;
            }
          }

          .operate {
            flex-direction: column;
          }
        }

        .right_line {
          border-left: 1px solid #E5E5E5;
        }
      }
    }
  }

  :deep(.el-pagination) {
    margin-top: 40px;
    text-align: right;

    .el-pager {
      li:not(.disabled):hover {
        color: $mainGlod;
      }

      li:not(.disabled).active {
        background-color: $mainGlod;
      }

      li:not(.disabled).active:hover {
        color: #F4F4F5;
      }
    }
  }
}
</style>
